#!/bin/sh
# PCP QA Test No. 1996
# Exercise the InfiniBand PMDA with mock libraries (no hardware required)
#
# Copyright (c) 2025 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -f $PCP_PMDAS_DIR/infiniband/pmdaib ] || _notrun "infiniband pmda not installed"

status=1	# failure is the default!
trap "cd $here; rm -rf $tmp.*; exit \$status" 0 1 2 3 15

# Check for mock IB libraries
test -f $here/src/libibumad.$DSO_SUFFIX || \
	_notrun "No InfiniBand mock umad library is available"
test -f $here/src/libibverbs.$DSO_SUFFIX || \
	_notrun "No InfiniBand mock verbs library is available"

_filter()
{
    tee -a $seq_full \
    | sed \
	-e 's/.* pminfo\(.*\) Info:/pminfo[PID] Info:/g' \
	-e 's/inst \[[0-9][0-9]* /inst [N /g' \
	-e '/No IB ports found to monitor/d' \
	-e '/Unknown parameter/d' \
    #end
}

pmda=$PCP_PMDAS_DIR/infiniband/pmda_ib.$DSO_SUFFIX,ib_init
pmns=$PCP_PMDAS_DIR/infiniband/root
domain=91
pminfo -L -K clear -K add,$domain,$pmda -d -n $pmns infiniband.hca.numports >$tmp.tmp 2>&1
echo "-- initial test for IB support --" >>$seq_full
cat $tmp.tmp >>$seq_full
if grep -q 'No IB kernel support or incorrect ABI version' $tmp.tmp
then
    _notrun "No InfiniBand support here"
fi

# real QA test starts here
root=$tmp.root
export IB_STATSPATH=$root

# Create versioned symlinks for mock libraries so the PMDA can find them
# Determine version numbers from what the PMDA actually needs
cd $here/src
if [ -f $PCP_PMDAS_DIR/infiniband/pmda_ib.$DSO_SUFFIX ]; then
    # Extract library versions that the PMDA links against
    for lib in $(ldd $PCP_PMDAS_DIR/infiniband/pmda_ib.$DSO_SUFFIX 2>/dev/null | \
                 grep -E 'libibmad|libibumad|libibverbs' | awk '{print $1}'); do
        case "$lib" in
            libibumad.so.*)
                umad_ver=$(echo $lib | sed 's/libibumad\.so\.//')
                ln -sf libibumad.$DSO_SUFFIX libibumad.$DSO_SUFFIX.$umad_ver
                ;;
            libibmad.so.*)
                mad_ver=$(echo $lib | sed 's/libibmad\.so\.//')
                # Our umad mock also provides mad functions
                ln -sf libibumad.$DSO_SUFFIX libibmad.$DSO_SUFFIX.$mad_ver
                ;;
            libibverbs.so.*)
                verbs_ver=$(echo $lib | sed 's/libibverbs\.so\.//')
                ln -sf libibverbs.$DSO_SUFFIX libibverbs.$DSO_SUFFIX.$verbs_ver
                ;;
        esac
    done
fi
cd $here

export LD_LIBRARY_PATH=$here/src:$LD_LIBRARY_PATH

for tgz in $here/infiniband/ib-root-*.tgz
do
    rm -fr $root
    mkdir $root || _fail "root in use when processing $tgz"
    cd $root
    tar xzf $tgz
    base=`basename $tgz`

    echo
    echo "== Testing InfiniBand PMDA with mock libraries - $base ==" | tee -a $seq_full

    # Test basic metric fetching
    echo
    echo "=== HCA (Host Channel Adapter) metrics ===" | tee -a $seq_full
    metrics="infiniband.hca.numports infiniband.hca.res.pd"
    pminfo -L -K clear -K add,$domain,$pmda -dfmtT -n $pmns $metrics 2>&1 | _filter

    echo
    echo "=== Port state metrics ===" | tee -a $seq_full
    metrics=infiniband.port.state
    pminfo -L -K clear -K add,$domain,$pmda -dfmtT -n $pmns $metrics 2>&1 | _filter

    echo
    echo "=== Port performance counters ===" | tee -a $seq_full
    metrics="infiniband.port.in.bytes infiniband.port.out.bytes"
    pminfo -L -K clear -K add,$domain,$pmda -dfmtT -n $pmns $metrics 2>&1 | _filter

    echo
    echo "=== Sysfs-based metrics ===" | tee -a $seq_full
    metrics="infiniband.hca.board_id infiniband.port.node_desc"
    pminfo -L -K clear -K add,$domain,$pmda -dfmtT -n $pmns $metrics 2>&1 | _filter

    ( echo && echo "== done" && echo ) | tee -a $seq_full
    cd $here
done

# success, all done
status=0
exit
